{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Focal_Loss import focal_loss\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred: tensor([[-0.1329,  0.2031,  1.3218, -0.4981, -0.0344],\n",
      "        [ 0.9031, -0.1756,  0.3095,  0.0399,  1.7733],\n",
      "        [ 2.0802, -0.7316, -0.9271, -0.3036, -0.5895]])\n",
      "label: tensor([2, 3, 4])\n"
     ]
    }
   ],
   "source": [
    "pred = torch.randn((3,5))\n",
    "print(\"pred:\",pred)\n",
    "\n",
    "label = torch.tensor([2,3,4])\n",
    "print(\"label:\",label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " --- Focal_loss alpha = 0.25 ,将对背景类进行衰减,请在目标检测任务中使用 --- \n",
      "tensor(1.1915)\n"
     ]
    }
   ],
   "source": [
    "# alpha设定为0.25,对第一类影响进行减弱(目标检测任务中,第一类为背景类)\n",
    "loss_fn = focal_loss(alpha=0.25, gamma=2, num_classes=5)\n",
    "loss = loss_fn(pred, label)\n",
    "print(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " --- Focal_loss alpha = [1, 2, 3, 1, 2], 将对每一类权重进行精细化赋值 --- \n",
      "tensor(2.5672)\n"
     ]
    }
   ],
   "source": [
    "# alpha输入列表,分别对每一类施加不同的权重\n",
    "loss_fn = focal_loss(alpha=[1,2,3,1,2], gamma=2, num_classes=5)\n",
    "loss = loss_fn(pred, label)\n",
    "print(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred: tensor([[-0.1329,  0.2031,  1.3218, -0.4981, -0.0344],\n",
      "        [ 0.9031, -0.1756,  0.3095,  0.0399,  1.7733],\n",
      "        [ 2.0802, -0.7316, -0.9271, -0.3036, -0.5895]], device='cuda:0')\n",
      "label: tensor([2, 3, 4], device='cuda:0')\n",
      " --- Focal_loss alpha = [1, 2, 3, 1, 2], 将对每一类权重进行精细化赋值 --- \n",
      "tensor(2.5672, device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "# GPU调用\n",
    "pred = pred.to('cuda')\n",
    "print(\"pred:\",pred)\n",
    "\n",
    "label = label.to('cuda')\n",
    "print(\"label:\",label)\n",
    "loss_fn = focal_loss(alpha=[1,2,3,1,2], gamma=2, num_classes=5)\n",
    "loss = loss_fn(pred, label)\n",
    "print(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
